<template>
  <div class="user">
    <!--搜索表单-->
    <div class="common-seach-wrap">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="">
          <el-input v-model="formInline.name" placeholder="请输入厂家名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="getTableInfo()">查询</el-button>
          <el-button type="primary" icon="Plus" @click="handleOpen({})">添加厂家</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData" row-key="id" height="100%" stripe>
      <el-table-column type="index" label="#" fixed="left" />
      <el-table-column prop="id" label="ID" fixed="left" />
      <el-table-column prop="factory_name" label="厂家" fixed="left" />
      <el-table-column prop="" label="图标">
        <template #default="scope">
          <el-image :src="scope.row.logo" alt="" style="width: 50px; height: 50px" />
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="200">
        <template #default="scope">
          <el-button type="primary" icon="Edit" @click="handleOpen(scope.row)"> 编辑 </el-button>
          <el-button type="danger" icon="DeleteFilled" @click="delInfo(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-divider border-style="none" />
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      background
      :current-page="curPage"
      :page-size="pageSize"
      layout="total, sizes,prev, pager, next, jumper"
      :total="total"
    ></el-pagination>
    <Dialog ref="infoRef" @getTable="getTableInfo" />
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, reactive } from "vue";
import Dialog from "./component/dialogForm.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { getInfoListAPi, deleteApi } from "@/api/carfilm/factory";
const formInline = reactive({
  name: "",
});
const infoRef = ref<any>(null);
const total = ref(0);
const curPage = ref(1);
const pageSize = ref(20);
const tableData = ref([]);
onMounted(() => {
  getTableInfo();
});
const handleSizeChange = (val: number) => {
  pageSize.value = val;
  getTableInfo();
};
const handleCurrentChange = (val: number) => {
  curPage.value = val;
  getTableInfo();
};
function getTableInfo() {
  getInfoListAPi({
    page: curPage.value,
    limit: pageSize.value,
    ...formInline,
  }).then((res: any) => {
    tableData.value = res.data.data;
    total.value = res.data.total;
  });
}

function delInfo(id: number) {
  ElMessageBox.confirm("确认要删除此厂家吗?", "警告！", {
    confirmButtonText: "确认删除",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteApi(id).then((res: any) => {
        getTableInfo();

        ElMessage({
          type: "success",
          message: "删除成功！",
        });
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "删除已取消",
      });
    });
}

// 打开添加数据窗口
const emits = defineEmits(["openForm", "search"]);
function handleOpen(row?: any) {
  infoRef.value.dialogOpen(row);
}
</script>

<style lang="scss">
.el-tabs {
  .el-tabs__new-tab {
    width: auto !important;
  }
}
.table-box {
  height: calc(100% - 200px);
}
</style>
